package com.aaron.kjviewutils.driving;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aaron.kjviewutils.BaseActivity;
import com.aaron.kjviewutils.R;
import com.aaron.kjviewutils.RecycleViewDividerForList;
import com.aaron.kjviewutils.retrofit.RetrofitUtil;
import com.aaron.kjviewutils.spinkit.PoponDismissListener;
import com.aaron.kjviewutils.stock.MsgPopWin;
import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Aaron on 2017/3/3.
 */

public class DrivingDocActivity extends BaseActivity implements PullLoadMoreRecyclerView.PullLoadMoreListener {

    @Bind(R.id.drivingDocRecyclerView)
    PullLoadMoreRecyclerView drivingDocLoadMoreRecyclerView;
    @Bind(R.id.drivingDocToolBar)
    Toolbar drivingDocToolBar;
    @Bind(R.id.barTitle)
    TextView barTitle;
    @Bind(R.id.leftBarIcon)
    ImageView leftBarIcon;

    private String type;
    private RecyclerView drivingDocRecyclerView;
    private List<DrivingDocBean.DrivingDocInfo.DrivingDocInner> datas;
    private DrivingDocAdapter adapter;
    private Call<DrivingDocBean> kemu1Call;
    private Call<DrivingDocBean> kemu4Call;
    private Call<DrivingDocBean> zhuantiCall;
    private DrivingDocService drivingDocService;
    private int curPage = 1;
    private int pageSize = 10;
    private String cid;
    private MsgPopWin msgPop;

    @OnClick(R.id.leftBarIcon)
    void leftBarIconClick()
    {
        closeSelf(DrivingDocActivity.this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = this.getIntent().getExtras();
        type = bundle.getString("type");
        if(bundle.containsKey("cid"))
        {
            cid = bundle.getString("cid");
        }
        this.setContentView(R.layout.layout_driving_doc);
        ButterKnife.bind(this);
        initTitle();
        msgPop = new MsgPopWin(DrivingDocActivity.this, DrivingDocActivity.this);
        if(null==retrofit)
        {
            retrofit = RetrofitUtil.obtainRetrofit();
        }
        drivingDocService = retrofit.create(DrivingDocService.class);
        initRecyclerView();
    }

    private void initRecyclerView()
    {
        drivingDocRecyclerView = drivingDocLoadMoreRecyclerView.getRecyclerView();
        drivingDocRecyclerView.addItemDecoration(new RecycleViewDividerForList(
                DrivingDocActivity.this, LinearLayoutManager.HORIZONTAL));
        datas = new ArrayList<DrivingDocBean.DrivingDocInfo.DrivingDocInner>();
        //这里用线性显示 类似于listview
        adapter = new DrivingDocAdapter(DrivingDocActivity.this, datas);
        drivingDocRecyclerView.setVerticalScrollBarEnabled(true);
        //显示下拉刷新
        drivingDocLoadMoreRecyclerView.setRefreshing(true);
        //设置上拉刷新文字
        drivingDocLoadMoreRecyclerView.setFooterViewBackgroundColor(android.R.color.transparent);
        //设置上拉刷新文字颜色
        //mPullLoadMoreRecyclerView.setFooterViewTextColor(R.color.white);
        //设置加载更多背景色
        //mPullLoadMoreRecyclerView.setFooterViewBackgroundColor(R.color.colorBackground);
        drivingDocLoadMoreRecyclerView.setLinearLayout();

        drivingDocLoadMoreRecyclerView.setOnPullLoadMoreListener(this);
        drivingDocLoadMoreRecyclerView.setAdapter(adapter);
        drivingDocLoadMoreRecyclerView.refresh();
        adapter.setmOnItemClickListener(new DrivingDocAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                DrivingDocBean.DrivingDocInfo.DrivingDocInner drivingDoc = datas.get(position);
                msgPop.showUi(drivingDoc, MsgPopWin.MsgType.MSG_DRIVING_DOC);
                msgPop.showAtLocation(drivingDocToolBar, Gravity.CENTER, 0, 0);
                msgPop.setOnDismissListener(new PoponDismissListener(DrivingDocActivity.this));
            }
        });
    }

    private void initTitle()
    {
        drivingDocToolBar.setTitle("");
        if("no.1".equals(type))
        {
            barTitle.setText("科目一题库");
        }
        else if("no.3".equals(type))
        {
            barTitle.setText("专项题库练习");
        }
        else if("no.4".equals(type))
        {
            barTitle.setText("科目四题库");
        }
        loadBackground(leftBarIcon, resources.getDrawable(R.mipmap.back));
        drivingDocToolBar.setNavigationIcon(null);
    }

    Runnable runnableReflush = new Runnable() {
        @Override
        public void run() {

            if(null!=drivingDocService)
            {
                if(1!=curPage)
                {
                    curPage = 1;
                }
                Map<String, Object> params = new HashMap<String, Object>();
                if("no.1".equals(type))
                {
                    //科目一题库下拉刷新
                    params.put("page", curPage);
                    params.put("size", pageSize);
                    kemu1Call = drivingDocService.obtainKemu1(params);
                    final Call<DrivingDocBean> kemu1ReflushCall = kemu1Call.clone();
                    kemu1ReflushCall.enqueue(new Callback<DrivingDocBean>() {
                        @Override
                        public void onResponse(Call<DrivingDocBean> call, Response<DrivingDocBean> response) {
                            kemu1ReflushCall.cancel();
                            drivingDocLoadMoreRecyclerView.setPullLoadMoreCompleted();
                            DrivingDocBean drivingDoc = response.body();
                            if(null!=drivingDoc&&null!=drivingDoc.result)
                            {
                                if("200".equals(drivingDoc.retCode))
                                {
                                    List<DrivingDocBean.DrivingDocInfo.DrivingDocInner> drivingDocInners = drivingDoc.result.list;
                                    if(null!=drivingDocInners&&!drivingDocInners.isEmpty())
                                    {
                                        datas.clear();
                                        datas.addAll(drivingDocInners);
                                        adapter.notifyDataSetChanged();
                                    }
                                    else
                                    {
                                        showMsg("获取科目1题库信息失败");
                                    }
                                }
                                else
                                {
                                    showMsg(drivingDoc.msg);
                                }
                            }
                            else
                            {
                                showMsg("获取科目1题库信息失败");
                            }
                        }

                        @Override
                        public void onFailure(Call<DrivingDocBean> call, Throwable t) {
                            kemu1ReflushCall.cancel();
                            drivingDocLoadMoreRecyclerView.setPullLoadMoreCompleted();
                            showMsg("获取科目1题库信息失败");
                        }
                    });
                }
                else if("no.3".equals(type))
                {
                    //专项题库分类下拉刷新
                    params.put("page", curPage);
                    params.put("size", pageSize);
                    params.put("cid", cid);
                    zhuantiCall = drivingDocService.obtainZhuanti(params);
                    final Call<DrivingDocBean> zhuantiflushCall = zhuantiCall.clone();
                    zhuantiflushCall.enqueue(new Callback<DrivingDocBean>() {
                        @Override
                        public void onResponse(Call<DrivingDocBean> call, Response<DrivingDocBean> response) {
                            zhuantiflushCall.cancel();
                            drivingDocLoadMoreRecyclerView.setPullLoadMoreCompleted();
                            DrivingDocBean drivingDoc = response.body();
                            if(null!=drivingDoc&&null!=drivingDoc.result)
                            {
                                if("200".equals(drivingDoc.retCode))
                                {
                                    List<DrivingDocBean.DrivingDocInfo.DrivingDocInner> drivingDocInners = drivingDoc.result.list;
                                    if(null!=drivingDocInners&&!drivingDocInners.isEmpty())
                                    {
                                        datas.clear();
                                        datas.addAll(drivingDocInners);
                                        adapter.notifyDataSetChanged();
                                    }
                                    else
                                    {
                                        showMsg("获取专题题库信息失败");
                                    }
                                }
                                else
                                {
                                    showMsg(drivingDoc.msg);
                                }
                            }
                            else
                            {
                                showMsg("获取专题题库信息失败");
                            }
                        }

                        @Override
                        public void onFailure(Call<DrivingDocBean> call, Throwable t) {
                            zhuantiflushCall.cancel();
                            drivingDocLoadMoreRecyclerView.setPullLoadMoreCompleted();
                            showMsg("获取专题题库信息失败");
                        }
                    });
                }
                else if("no.4".equals(type))
                {
                    //科目四题库下拉刷新
                    params.put("page", curPage);
                    params.put("size", pageSize);
                    kemu4Call = drivingDocService.obtainKemu4(params);
                    final Call<DrivingDocBean> kemu4ReflushCall = kemu4Call.clone();
                    kemu4ReflushCall.enqueue(new Callback<DrivingDocBean>() {
                        @Override
                        public void onResponse(Call<DrivingDocBean> call, Response<DrivingDocBean> response) {
                            kemu4ReflushCall.cancel();
                            drivingDocLoadMoreRecyclerView.setPullLoadMoreCompleted();
                            DrivingDocBean drivingDoc = response.body();
                            if(null!=drivingDoc&&null!=drivingDoc.result)
                            {
                                if("200".equals(drivingDoc.retCode))
                                {
                                    List<DrivingDocBean.DrivingDocInfo.DrivingDocInner> drivingDocInners = drivingDoc.result.list;
                                    if(null!=drivingDocInners&&!drivingDocInners.isEmpty())
                                    {
                                        datas.clear();
                                        datas.addAll(drivingDocInners);
                                        adapter.notifyDataSetChanged();
                                    }
                                    else
                                    {
                                        showMsg("获取科目4题库信息失败");
                                    }
                                }
                                else
                                {
                                    showMsg(drivingDoc.msg);
                                }
                            }
                            else
                            {
                                showMsg("获取科目4题库信息失败");
                            }
                        }

                        @Override
                        public void onFailure(Call<DrivingDocBean> call, Throwable t) {
                            kemu4ReflushCall.cancel();
                            drivingDocLoadMoreRecyclerView.setPullLoadMoreCompleted();
                            showMsg("获取科目4题库信息失败");
                        }
                    });
                }
            }
        }
    };

    Runnable runnableLoadMore = new Runnable() {
        @Override
        public void run() {
            if(null!=drivingDocService)
            {
                curPage=curPage+1;
                Map<String, Object> params = new HashMap<String, Object>();
                if("no.1".equals(type))
                {
                    //科目一题库上拉加载
                    params.put("page", curPage);
                    params.put("size", pageSize);
                    kemu1Call = drivingDocService.obtainKemu1(params);
                    final Call<DrivingDocBean> kemu1LoadCall = kemu1Call.clone();
                    kemu1LoadCall.enqueue(new Callback<DrivingDocBean>() {
                        @Override
                        public void onResponse(Call<DrivingDocBean> call, Response<DrivingDocBean> response) {
                            kemu1LoadCall.cancel();
                            drivingDocLoadMoreRecyclerView.setPullLoadMoreCompleted();
                            DrivingDocBean drivingDoc = response.body();
                            if(null!=drivingDoc&&null!=drivingDoc.result)
                            {
                                if("200".equals(drivingDoc.retCode))
                                {
                                    List<DrivingDocBean.DrivingDocInfo.DrivingDocInner> drivingDocInners = drivingDoc.result.list;
                                    if(null!=drivingDocInners&&!drivingDocInners.isEmpty())
                                    {
                                        datas.addAll(drivingDocInners);
                                        adapter.notifyDataSetChanged();
                                    }
                                    else
                                    {
                                        showMsg("获取科目1题库信息失败");
                                    }
                                }
                                else
                                {
                                    showMsg(drivingDoc.msg);
                                }
                            }
                            else
                            {
                                showMsg("获取科目1题库信息失败");
                            }
                        }

                        @Override
                        public void onFailure(Call<DrivingDocBean> call, Throwable t) {
                            kemu1LoadCall.cancel();
                            drivingDocLoadMoreRecyclerView.setPullLoadMoreCompleted();
                            showMsg("获取科目1题库信息失败");
                        }
                    });
                }
                else if("no.3".equals(type))
                {
                    //专题题库上拉加载
                    params.put("page", curPage);
                    params.put("size", pageSize);
                    params.put("cid", cid);
                    zhuantiCall = drivingDocService.obtainZhuanti(params);
                    final Call<DrivingDocBean> zhuantiLoadCall = zhuantiCall.clone();
                    zhuantiLoadCall.enqueue(new Callback<DrivingDocBean>() {
                        @Override
                        public void onResponse(Call<DrivingDocBean> call, Response<DrivingDocBean> response) {
                            zhuantiLoadCall.cancel();
                            drivingDocLoadMoreRecyclerView.setPullLoadMoreCompleted();
                            DrivingDocBean drivingDoc = response.body();
                            if(null!=drivingDoc&&null!=drivingDoc.result)
                            {
                                if("200".equals(drivingDoc.retCode))
                                {
                                    List<DrivingDocBean.DrivingDocInfo.DrivingDocInner> drivingDocInners = drivingDoc.result.list;
                                    if(null!=drivingDocInners&&!drivingDocInners.isEmpty())
                                    {
                                        datas.addAll(drivingDocInners);
                                        adapter.notifyDataSetChanged();
                                    }
                                    else
                                    {
                                        showMsg("获取专题题库信息失败");
                                    }
                                }
                                else
                                {
                                    showMsg(drivingDoc.msg);
                                }
                            }
                            else
                            {
                                showMsg("获取专题题库信息失败");
                            }
                        }

                        @Override
                        public void onFailure(Call<DrivingDocBean> call, Throwable t) {
                            zhuantiLoadCall.cancel();
                            drivingDocLoadMoreRecyclerView.setPullLoadMoreCompleted();
                            showMsg("获取专题题库信息失败");
                        }
                    });
                }
                else if("no.4".equals(type))
                {
                    //科目四题库上拉加载
                    params.put("page", curPage);
                    params.put("size", pageSize);
                    kemu4Call = drivingDocService.obtainKemu4(params);
                    final Call<DrivingDocBean> kemu4LoadCall = kemu4Call.clone();
                    kemu4LoadCall.enqueue(new Callback<DrivingDocBean>() {
                        @Override
                        public void onResponse(Call<DrivingDocBean> call, Response<DrivingDocBean> response) {
                            kemu4LoadCall.cancel();
                            drivingDocLoadMoreRecyclerView.setPullLoadMoreCompleted();
                            DrivingDocBean drivingDoc = response.body();
                            if(null!=drivingDoc&&null!=drivingDoc.result)
                            {
                                if("200".equals(drivingDoc.retCode))
                                {
                                    List<DrivingDocBean.DrivingDocInfo.DrivingDocInner> drivingDocInners = drivingDoc.result.list;
                                    if(null!=drivingDocInners&&!drivingDocInners.isEmpty())
                                    {
                                        datas.addAll(drivingDocInners);
                                        adapter.notifyDataSetChanged();
                                    }
                                    else
                                    {
                                        showMsg("获取科目4题库信息失败");
                                    }
                                }
                                else
                                {
                                    showMsg(drivingDoc.msg);
                                }
                            }
                            else
                            {
                                showMsg("获取科目4题库信息失败");
                            }
                        }

                        @Override
                        public void onFailure(Call<DrivingDocBean> call, Throwable t) {
                            kemu4LoadCall.cancel();
                            drivingDocLoadMoreRecyclerView.setPullLoadMoreCompleted();
                            showMsg("获取科目4题库信息失败");
                        }
                    });
                }
            }
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN)
        {
            //关闭
            closeSelf(DrivingDocActivity.this);
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(null!=mHandler)
        {
            mHandler.removeCallbacks(runnableReflush);
            mHandler.removeCallbacks(runnableLoadMore);
        }
        if(null!=msgPop)
        {
            msgPop.dismissView();
        }
        if(null!=drivingDocLoadMoreRecyclerView)
        {
            drivingDocLoadMoreRecyclerView.cancelLongPress();
        }
        if(null!=kemu1Call)
        {
            kemu1Call.cancel();
        }
        if(null!=kemu4Call)
        {
            kemu4Call.cancel();
        }
        if(null!=zhuantiCall)
        {
            zhuantiCall.cancel();
        }
        ButterKnife.unbind(this);
    }

    @Override
    public void onRefresh() {
        mHandler.postDelayed(runnableReflush, 1000);
    }

    @Override
    public void onLoadMore() {
        mHandler.postDelayed(runnableLoadMore, 1000);
    }
}
